package counter.console;

import counter.counter.Counter;
import counter.io.ConsoleOutput;

public abstract class ConsoleCommand {
    private String description;

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    abstract public void execute(String... params);

    static void assertParamCount(String[] params, int assertCount) {
        if ((assertCount == 0) && (params != null) && (params.length != 0)) {
            throw new IllegalArgumentException("Parameters not allowed in this command");
        }
        if ((assertCount > 0) && ((params == null) || (params.length == 0))) {
            throw new IllegalArgumentException("This command require parameters. Type help for details");
        }
        if (params != null && assertCount != params.length) {
            throw new IllegalArgumentException("Wrong parameters count: "
                    + params.length + "; required: " + assertCount);
        }
    }
}


class CalculateSummaryInfoCommand extends ConsoleCommand {
    CalculateSummaryInfoCommand() {
        setDescription("Calculation summary info for specified root folder");
    }

    @Override
    public void execute(String... params) {
        assertParamCount(params, 1);
        Counter.getInstance().calculate(params[0]);
    }
}

class ConsolePrintCommand extends ConsoleCommand {
    ConsolePrintCommand() {
        setDescription("Print previously calculaed results on console");
    }

    @Override
    public void execute(String... params) {
        ConsoleOutput.generateConsoleOutput(Counter.getInstance().getCounterInfo());
    }

}

class HelpCommand extends ConsoleCommand {
    HelpCommand() {
        setDescription("Help command");
    }
    @Override
    public void execute(String... params) {

    }
}

class QuitCommand extends ConsoleCommand {
    QuitCommand() {
        setDescription("Quit command");
    }
    @Override
    public void execute(String... params){
        assertParamCount(params, 0);
        System.exit(0);
    }
}
